package controleur;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;

import servlet.AjouterAmiServlet;
import servlet.AjouterCommentaireServlet;
import servlet.AjouterEnvieServlet;
import servlet.ConnexionServlet;
import servlet.DeconnexionServlet;
import servlet.DemandesAmiServlet;
import servlet.DesinscriptionServlet;
import servlet.FriendListServlet;
import servlet.IndexServlet;
import servlet.InscriptionServlet;
import servlet.ModificationUserServlet;
import servlet.NotificationServlet;
import servlet.ProfilServlet;
import servlet.RechercheServlet;
import servlet.SupprimerAmiServlet;
import servlet.SupprimerCommentaireServlet;
import servlet.SupprimerEnvieServlet;
import servlet.VisiterMurServlet;
import ejb.Facade;

/**
 * Servlet implementation class Controleur
 */
@WebServlet("/Controleur")
public class Controleur extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Controleur() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		Facade fac = (Facade) session.getAttribute("Facade");
		if (fac == null) {
			Context ctx;
			try {
				ctx = new InitialContext();
				fac = (Facade) ctx.lookup("java:module/FacadeBean");
				session.setAttribute("Facade", fac);
			} catch (NamingException e) {
				e.printStackTrace();
			}
		}
		User user = (User)session.getAttribute("user");
		if(user != null) {
			fac.refreshEntity();
			user = fac.getUser(user.getIdUser());
			request.getSession().setAttribute("user", user);
		}

		String action = request.getParameter("action");
		if ((action == null) || (action.equals("accueil"))) {
			new IndexServlet(request, response);
		} else if (action.equals("inscription")) {
			new InscriptionServlet(request, response);
		} else if (action.equals("connexion")) {
			new ConnexionServlet(request, response);
		} else if (action.equals("addUser")) {
			new InscriptionServlet(request, response);
		} else if (action.equals("connection")) {
			new ConnexionServlet(request, response);
		} else if (action.equals("deconnexion")) {
			new DeconnexionServlet(request, response);
		} else if (action.equals("search")) {
			new RechercheServlet(request, response);
		} else if (action.equals("profil")) {
			new ProfilServlet(request, response);
		} else if (action.equals("modification")) {
			new ModificationUserServlet(request, response);
		} else if (action.equals("amis")) {
			new FriendListServlet(request, response);
		} else if (action.equals("newWish")) {
			new AjouterEnvieServlet(request, response);
		} else if (action.equals("removeWish")) {
			new SupprimerEnvieServlet(request, response);
		} else if (action.equals("visiter")) {
			new VisiterMurServlet(request, response);
		} else if (action.equals("addFriend")) {
			new AjouterAmiServlet(request, response);
		} else if (action.equals("addCom")) {
			new AjouterCommentaireServlet(request, response);
		} else if (action.equals("notification")) {
			new NotificationServlet(request, response);
		} else if (action.equals("supprimerAmi")) {
			new SupprimerAmiServlet(request, response);
		} else if (action.equals("supprimerCommentaire")) {
			new SupprimerCommentaireServlet(request, response);
		} else if(action.equals("demande") || action.equals("accept") || action.equals("refuse")) {
			new DemandesAmiServlet(request, response);
		} else if (action.equals("desinscription")) {
			new DesinscriptionServlet(request, response);
		} else {
			new IndexServlet(request, response);
		}
	}

}
